package cn.kgc.jinxin.clothesstore.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import cn.kgc.jinxin.clothesstore.dao.AdminDao;
import cn.kgc.jinxin.clothesstore.dao.BaseDao;
import cn.kgc.jinxin.clothesstore.entity.Admin;
import cn.kgc.jinxin.clothesstore.entity.Category;

public class AdminDaoImpl implements AdminDao{

	@Override
	public List<Admin> selectAll() throws Exception {
		List<Admin> adminList = new ArrayList<>();
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		try {
			connection = BaseDao.getConnection();
			String sql = "select adminName,adminPwd,AdmindateTime from Admin";
			preparedStatement = connection.prepareStatement(sql);
			resultSet = preparedStatement.executeQuery();
			Admin admin;
			while (resultSet.next()) {
				admin= new Admin();
				admin.setAdminName(resultSet.getString(1));
				admin.setAdminPwd(resultSet.getString(2));
				admin.setDateTime(resultSet.getDate(3));
				adminList.add(admin);
			} 
		} finally {
			BaseDao.close(connection, preparedStatement, resultSet);
		}
		return adminList;
	}

	@Override
	public Admin selectByName(String adminName,String adminPwd) throws Exception {
		Admin admin = null;
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		try {
			connection = BaseDao.getConnection();
			String sql = "select adminname,datetime from admin where adminname=? and adminpwd=?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, adminName);
			preparedStatement.setString(2, adminPwd);
			resultSet = preparedStatement.executeQuery();
			if (resultSet.next()) {
				admin = new Admin();
				admin.setAdminName(resultSet.getString(1));
				admin.setDateTime(resultSet.getDate(2));
			} 
		} finally {
			BaseDao.close(connection, preparedStatement, resultSet);
		}
		return admin;
	}

	@Override
	public void insert(Admin admin) throws Exception {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = BaseDao.getConnection();
			String sql = "insert into admin(adminName,adminPwd) VALUES (?,?)";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, admin.getAdminName());
			preparedStatement.setString(2, admin.getAdminPwd());
			preparedStatement.executeUpdate();
		} finally {
			BaseDao.close(connection, preparedStatement, null);
		}		
	}

	@Override
	public void update(Admin admin,String newAdminPwd) throws Exception {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = BaseDao.getConnection();
			String sql = "update admin set adminpwd=? where adminName and adminpwd=?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, newAdminPwd);
			preparedStatement.setString(2, admin.getAdminName());
			preparedStatement.setString(3, admin.getAdminPwd());
			preparedStatement.executeUpdate();
		} finally {
			BaseDao.close(connection, preparedStatement, null);
		}
		
	}

	@Override
	public void deleteByName(String adminName) throws Exception {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = BaseDao.getConnection();
			String sql = "delete from admin where adminname=?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, adminName);
			preparedStatement.executeUpdate();
		} finally {
			BaseDao.close(connection, preparedStatement, null);
		}
	}

}
